﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;


namespace EmailStorm.Data
{
    public class Email
    {

        
        public  void AddEmail(string email,string provider,string orignUrl,string site,int taskId){
            if (!IsEmailExist(email)) {
                string sql = @"insert into email (email_address,provider,create_date,orign_url,site,task_id)
                    values (@email_address,@provider,@create_date,@orign_url,@site,@task_id)";
                SqliteAccess obj = new SqliteAccess();
                SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("email_address",email),
                new SQLiteParameter("provider",provider),
                new SQLiteParameter("create_date",DateTime.Now),
                new SQLiteParameter("orign_url",orignUrl),
                new SQLiteParameter("site",site),
                new SQLiteParameter("task_id",taskId)
            };
                obj.ExecuteNonQuery(sql, parameters);
            }
            
        }


        public void DeleteEmail(string email) {
            string sql = "delete from email where email_address=@email_address";
            SqliteAccess obj = new SqliteAccess();
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("email_address",email)
            };
            obj.ExecuteNonQuery(sql,parameters);
        }

        public DataSet GetEmailList(int taskId) {
            string sql = "select * from email where task_id=@task_id";
            SqliteAccess obj = new SqliteAccess();
            SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("task_id",taskId)
            };
            return obj.ExecuteDataSet(sql, parameters);
        }


        public bool IsEmailExist(string email) {
            string sql = @"select count(*) from email where email_address =@email_address";
             SqliteAccess obj = new SqliteAccess();
             SQLiteParameter[] parameters = new SQLiteParameter[]{
                new SQLiteParameter("email_address",email)
            };
            string result=obj.ExecuteScalar(sql,parameters);
            int rowCount = Convert.ToInt32(result);
            return rowCount > 0;

        }
    }
}
